$(function () {

    var supplier_datagrid = $("#supplier_datagrid");
    var supplier_dialog = $("#supplier_dialog");
    var supplier_form = $("#supplier_form");


    /*easyui的数据列表组件初始化*/
    supplier_datagrid.datagrid({
        url: "/supplier/query.do",
        fit: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#supplier_toolbar",
        columns: [[
            {field: 'id', title: 'id', width: 100, checkbox: true},
            {field: 'sup', title: '供应商', width: 100},
            {field: 'name', title: '联系人', width: 100},
            {field: 'phone', title: '电话', width: 100},
            {field: 'inputTime', title: '录入时间', width: 100}
        ]]
    })

    /*弹出编辑框的初始化*/
    supplier_dialog.dialog({
        width: 270,
        height: 300,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            supplier_form.form('clear');
        }
    })

    /*事件对象*/
    var methodObj = {

        /*新增供应商按钮事件*/
        add: function () {
            supplier_dialog.dialog('setTitle', "新增供应商");
            supplier_dialog.dialog('open');
        },

        /*编辑供应商按钮事件*/
        edit: function () {
            supplier_dialog.dialog('setTitle', "编辑供应商");
            /*c查看是否有选中的供应商*/
            var row = supplier_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            /*数据回显*/
            supplier_form.form('load', row);
            /*打开弹出框*/
            supplier_dialog.dialog('open');
        },

        /*表单保存事件*/
        save: function () {
            $.messager.confirm("温馨提示", "确定要保存吗?", function () {
                supplier_form.form('submit', {
                    url: "/supplier/saveOrUpdate.do",
                    success: function (data) {
                        /*easyui的表单响应数据不是json,需要转换*/
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                supplier_dialog.dialog('close');
                                supplier_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error")
                        }
                    }
                })
            })
        },
        /*删除角色事件*/
        del: function () {
            /*检查是否有选中的行*/
            var row = supplier_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "error");
                return;
            }
            /*删除操作*/
            $.messager.confirm("温馨提示", "确定要删除吗", function (tr) {
                if (tr) {
                    $.post("/supplier/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                supplier_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        },

        /*表单刷新按钮*/
        reload: function () {
            supplier_datagrid.datagrid("reload");
        },

        /*表单取消事件*/
        cancel: function () {
            supplier_dialog.dialog('close');
        }
    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })
})
